/*
* 请求有分页需求的数据操作对象
* (动态性代码的返回值是没有参数类型描述的)
* 接口返回json数据
   {
    "total":17,
    "count":10,
    "page":0,
    "total_page":2,
    "items":[
        {
            "id":27,
            "title":"Mier双色靠椅",
            "subtitle":"安静的午后，一杯清茶，追忆似水年华。看清风浮动，看落日余晖",
            "img":"http://i2.sleeve.7yue.pro/h1.png",
            "for_theme_img":"http://i1.sleeve.7yue.pro/assets/f6c9fce8-626f-44c0-a709-3f6ef9f3fbef.png",
            "price":"1299",
            "discount_price":null,
            "description":null,
            "tags":"",
            "sketch_spec_id":"1",
            "max_purchase_quantity":null,
            "min_purchase_quantity":null
        }
    ]
    }
*/
import {Http} from "./http";

class Paging {
    //全局变量
    url;//固化保存外部条用传进来的url
    reqParams;//请求网络参数
    start;//开始加载的初始
    count;//每次加载的总条数
    questLock = false;//数据请求锁，默认为false
    hasNext = true;//是否还有更多的数据
    callbackItems = [];//当前从返回参数中项

    //构造函数方法，在当前对象实力化的时候会被调用
    constructor(req, count = 10, start = 0) {
        console.log("Get a new Page.");
        this.reqParams = req;
        this.count = count;
        this.start = start;
        this.url = this.reqParams.url;
    }

    /*
    * 需要获取更多的数据
    * 1、为了防止在请求接口流程中重复请求获取更多数据
    * 需要增加锁的获取与释放
    * 执行流程
    * getLock
    * request
    * releaseLock
    */
    async getMoreData() {
        if (!this.hasNext || !this.getRequestLock()) {
            return;
        }
        let data = await this.doHttpGetData();
        this.releaseRequestLock();

        return data;
    }

    /*
    * 执行网络请求获取数据
    */
    async doHttpGetData() {
        const currentReq = this.dealWithUrl();
        console.log("the currentReq : " + currentReq);
        let paging = await Http.request(currentReq);
        if (!paging || paging.total === 0) {
            return {
                empty: true,
                hasNext: false,
                items: []
            }
        }

        this.hasNext = paging.data.total_page > paging.data.page + 1;
        console.log("paging.total_page : " + paging.data.total_page + " : paging.page : " + paging.data.page);
        console.log("the paging has hasNext : " + this.hasNext);
        //如果有更多的数据需要对start进行累加
        if (this.hasNext) {
            this.start += this.count;
        }
        this.addCallbackItems(paging.data.items);

        return {
            empty: false,
            hasNext: this.hasNext,
            items: this.callbackItems
        }
    }

    /*
    * 数组累加，在后面累计添加
    */
    addCallbackItems(items) {
        this.callbackItems = this.callbackItems.concat(items);
    }

    /*
    * 对传入的url进行拼接操作
    */
    dealWithUrl() {
        let url = this.url;
        const param = `start=${this.start}&count=${this.count}`;
        /*if (url.length < 0) {
            return;
        }*/

        if (url.includes('?')/*url.indexOf('?') !== -1*/) {
            url += '&' + param;
        } else {
            url += '?' + param;
        }

        this.reqParams.url = url;
        return this.reqParams;
    }

    /*
    * 获取数据请求锁
    */
    getRequestLock() {
        if (this.questLock) {
            return false;
        }

        this.questLock = true;
        return true;
    }

    /*
    * 释放数据请求锁
    */
    releaseRequestLock() {
        this.questLock = false;
        return false;
    }

}

export {
    Paging
}